#include <stdio.h>
#include <stdlib.h>
#include "ia042.h"
int main(int argc, char * * argv)
{
  int counts[NUMBER_LETTER] = {0}; 
  /* initialize all elements to zero */
  /* The number of English letters does not change so we can use an
     array of the fixed size */

  /* if argc is not 2, return EXIT_FAILURE */
  if (argc != 2)
    {
      printError(ARGC_ERROR);
      return EXIT_FAILURE;
    }
  /* open the input file whose name is given in argv[1] */
  FILE * fptr = fopen(argv[1], "r");
  
  /* if opening the file fails, return EXIT_FAILURE */
  if (fptr == NULL)
    {
      printError(FOPEN_ERROR);
      return EXIT_FAILURE;
    }
  
  /* count the occurrences of letters in the file */
  
  printCounts(counts);

  fclose (fptr);

  /* return EXIT_SUCCESS */
  return EXIT_SUCCESS;
}

void printError(int code)
{
  switch (code)
    {
    case ARGC_ERROR:
      printf("argc is not 2\n");
      break;
    case FOPEN_ERROR:
      printf("fopen fail\n");
      break;      
    case MALLOC_ERROR:
      printf("malloc fail\n");
      break;
    default:
      printf("unknown error code\n");
    }
}

void printCounts(int * counts)
{
  int iter;
  for (iter = 0; iter < NUMBER_LETTER; iter ++)
    {
      printf("%c: %d\n", iter + 'a', counts[iter]);
    }
}
